<?php
declare(strict_types=1);

namespace App\Utils\Common\Aes {

    use App\Exception\InternalException;
    use function Hyperf\Translation\__;

    /**
     * 加密
     * @param string $key
     * @param string $data
     * @return string
     */
    function encrypt(string $key, string $data, string $cipher = 'AES-128-ECB', int $options = OPENSSL_RAW_DATA): string
    {
//        $ivlen = openssl_cipher_iv_length($cipher);
//        $iv = !$ivlen ? '' : openssl_random_pseudo_bytes($ivlen);
        $result = openssl_encrypt($data, $cipher, $key, $options);
        if (!$result) {
            throw new InternalException(__('message.openssl.encrypt_failed'));
        }

        return $result;
    }

    /**
     * 解密
     * @param string $key
     * @param string $data
     * @param string $cipher
     * @return string|false
     */
    function decrypt(string $key, string $data, string $cipher = 'AES-128-ECB', int $options = OPENSSL_RAW_DATA): string|false
    {
        return openssl_decrypt($data, $cipher, $key, $options);
    }
}